package org.sevenless.vul;

import com.vaadin.ui.*;
import com.vaadin.terminal.Resource;
import java.util.Map;
import java.util.HashMap;
import java.lang.reflect.Method;

public class VulTags {

  private Map<String,VulAttributes> comps = new HashMap<String, VulAttributes>();
  private Map<String,VulAttributes> conts = new HashMap<String, VulAttributes>();

  public VulTags() {
    try {
      setupStandardComponent(com.vaadin.ui.Embedded.class);
      setupStandardComponent(com.vaadin.ui.Label.class);
      setupStandardComponent(com.vaadin.ui.Link.class);
      setupStandardComponent(com.vaadin.ui.MenuBar.class);
      setupStandardComponent(com.vaadin.ui.Upload.class);
      setupStandardComponent(com.vaadin.ui.Button.class);
      setupStandardComponent(com.vaadin.ui.CheckBox.class);
      setupStandardComponent(com.vaadin.ui.Form.class);
      setupStandardComponent(com.vaadin.ui.ProgressIndicator.class);
      setupStandardComponent(com.vaadin.ui.OptionGroup.class);
      setupStandardComponent(com.vaadin.ui.TwinColSelect.class);
      setupDateField();
      setupInlineDateField();
      setupPopupDateField();
      setupComboBox();
      setupListSelect();
      setupTextField();
      setupRichTextArea();
      setupTable();
      setupTree();
      setupSlider();
      
      setupStandardContainer(com.vaadin.ui.Accordion.class);
      setupWindow();
      setupPanel();
      setupSplitPanel();
      setupTabSheet();
      setupGridLayout();
      setupHorizontalLayout();
      setupVerticalLayout();
      setupAbsoluteLayout();
      setupCssLayout();
      setupFormLayout();
      
    } catch (Exception e) {
      //System.err.println("Error creating VulTags: "+e.getMessage());
      //e.printStackTrace(System.err);
    }
  }

  public boolean isContainer(String name) {
    return (conts.get(name) == null) ? false : true;
  }
  
  public boolean isComponent(String name) {
    return (comps.get(name) == null) ? false : true;
  }
  
  public VulAttributes getContainerAttr(String name) {
    return conts.get(name);
  }

  public VulAttributes getComponentAttr(String name) {
    return comps.get(name);
  }
  
  private void addStandardAttrs(Class c, VulAttributes va) throws Exception {
    va.addAttr("width",c.getMethod("setWidth",new Class[] {java.lang.String.class}));
    va.addAttr("height",c.getMethod("setHeight",new Class[] {java.lang.String.class}));
    va.addAttr("caption",c.getMethod("setCaption",new Class[] {java.lang.String.class}));
    va.addAttr("stylename",c.getMethod("setStyleName",new Class[] {java.lang.String.class}));
    va.addAttr("description",c.getMethod("setDescription",new Class[] {java.lang.String.class}));
    va.addAttr("enabled",c.getMethod("setEnabled",new Class[] {boolean.class}));
    va.addAttr("visible",c.getMethod("setVisible",new Class[] {boolean.class}));
    va.addAttr("sizefull",c.getMethod("setSizeFull",new Class[]{}));
    va.addAttr("icon",c.getMethod("setIcon",new Class[] {Resource.class}));
  }

  private VulAttributes setupStandardComponent(Class c) throws Exception {
    VulAttributes va = new VulAttributes(c);
    addStandardAttrs(c, va);
    comps.put(c.getSimpleName().toLowerCase(),va);
    return va;
  }

  private VulAttributes setupStandardContainer(Class c) throws Exception {
    VulAttributes va = new VulAttributes(c);
    addStandardAttrs(c, va);
    conts.put(c.getSimpleName().toLowerCase(),va);
    return va;
  }
  
  private void setupDateField() throws Exception {
    Class c = com.vaadin.ui.DateField.class;
    VulAttributes va = setupStandardComponent(c);
    va.addAttr("resolution",c.getMethod("setResolution",new Class[] {int.class}));
  }
  
  private void setupInlineDateField() throws Exception {
    Class c = com.vaadin.ui.InlineDateField.class;
    VulAttributes va = setupStandardComponent(c);
    va.addAttr("resolution",c.getMethod("setResolution",new Class[] {int.class}));
  }

  private void setupPopupDateField() throws Exception {
    Class c = com.vaadin.ui.PopupDateField.class;
    VulAttributes va = setupStandardComponent(c);
    va.addAttr("resolution",c.getMethod("setResolution",new Class[] {int.class}));
    va.addAttr("prompt",c.getMethod("setInputPrompt",new Class[] {java.lang.String.class}));
  }
  
  private void setupComboBox() throws Exception {
    Class c = com.vaadin.ui.ComboBox.class;
    VulAttributes va = setupStandardComponent(c);
    va.addAttr("multiselect",c.getMethod("setMultiSelect",new Class[] {boolean.class}));
    va.addAttr("allownull",c.getMethod("setNullSelectionAllowed",new Class[] {boolean.class}));
  }

  private void setupListSelect() throws Exception {
    Class c = com.vaadin.ui.ListSelect.class;
    VulAttributes va = setupStandardComponent(c);
    va.addAttr("rows",c.getMethod("setRows",new Class[] {int.class}));
    va.addAttr("cols",c.getMethod("setColumns",new Class[] {int.class}));
  }

  private void setupTextField() throws Exception {
    Class c = com.vaadin.ui.TextField.class;
    VulAttributes va = setupStandardComponent(c);
    va.addAttr("rows",c.getMethod("setRows",new Class[] {int.class}));
    va.addAttr("cols",c.getMethod("setColumns",new Class[] {int.class}));
    va.addAttr("secret",c.getMethod("setSecret",new Class[] {boolean.class}));
    va.addAttr("prompt",c.getMethod("setInputPrompt",new Class[] {java.lang.String.class}));
  }
  
  private void setupRichTextArea() throws Exception {
    Class c = com.vaadin.ui.RichTextArea.class;
    VulAttributes va = setupStandardComponent(c);
    va.addAttr("rows",c.getMethod("setRows",new Class[] {int.class}));
    va.addAttr("cols",c.getMethod("setColumns",new Class[] {int.class}));
    va.addAttr("secret",c.getMethod("setSecret",new Class[] {boolean.class}));
    va.addAttr("prompt",c.getMethod("setInputPrompt",new Class[] {java.lang.String.class}));
  }

  private void setupTable() throws Exception {
    Class c = com.vaadin.ui.Table.class;
    VulAttributes va = setupStandardComponent(c);
    va.addAttr("headermode",c.getMethod("setRowHeaderMode",new Class[] {int.class}));
    va.addAttr("editable",c.getMethod("setEditable",new Class[] {boolean.class}));
    va.addAttr("footer",c.getMethod("setFooterVisible",new Class[] {boolean.class}));
    va.addAttr("selectable",c.getMethod("setSelectable",new Class[] {boolean.class}));
    va.addAttr("unsorted",c.getMethod("setSortDisabled",new Class[] {boolean.class}));
  }

  private void setupTree() throws Exception {
    Class c = com.vaadin.ui.Tree.class;
    VulAttributes va = setupStandardComponent(c);
    va.addAttr("selectable",c.getMethod("setSelectable",new Class[] {boolean.class}));
  }
  
  private void setupSlider() throws Exception {
    Class c = com.vaadin.ui.Slider.class;
    VulAttributes va = setupStandardComponent(c);
    va.addAttr("orientation",c.getMethod("setOrientation",new Class[] {int.class}));
  }
  
  private void setupWindow() throws Exception {
    Class c = com.vaadin.ui.Window.class;
    VulAttributes va = setupStandardContainer(c);
    va.addAttr("scrollable",c.getMethod("setScrollable",new Class[] {boolean.class}));
    va.addAttr("draggable",c.getMethod("setDraggable",new Class[] {boolean.class}));
    va.addAttr("closable",c.getMethod("setClosable",new Class[] {boolean.class}));
    va.addAttr("resizable",c.getMethod("setResizable",new Class[] {boolean.class}));
    va.addAttr("x",c.getMethod("setPositionX",new Class[] {int.class}));
    va.addAttr("y",c.getMethod("setPositionY",new Class[] {int.class}));
    va.addAttr("border",c.getMethod("setBorder",new Class[] {int.class}));
  }

  private void setupPanel() throws Exception {
    Class c = com.vaadin.ui.Panel.class;
    VulAttributes va = setupStandardContainer(c);
    va.addAttr("scrollable",c.getMethod("setScrollable",new Class[] {boolean.class}));
  }

  private void setupSplitPanel() throws Exception {
    Class c = com.vaadin.ui.SplitPanel.class;
    VulAttributes va = setupStandardContainer(c);
    va.addAttr("orientation",c.getMethod("setOrientation",new Class[] {int.class}));
  }

  private void setupTabSheet() throws Exception {
    Class c = com.vaadin.ui.TabSheet.class;
    VulAttributes va = setupStandardContainer(c);
    va.addAttr("tabshidden",c.getMethod("hideTabs",new Class[] {boolean.class}));
  }

  private void setupGridLayout() throws Exception {
    Class c = com.vaadin.ui.GridLayout.class;
    VulAttributes va = setupStandardContainer(c);
    va.addAttr("rows",c.getMethod("setRows",new Class[] {int.class}));
    va.addAttr("cols",c.getMethod("setColumns",new Class[] {int.class}));
    va.addAttr("margin",c.getMethod("setMargin",new Class[] {Layout.MarginInfo.class}));
  }

  private void setupHorizontalLayout() throws Exception {
    Class c = com.vaadin.ui.HorizontalLayout.class;
    VulAttributes va = setupStandardContainer(c);
    va.addAttr("spacing",c.getMethod("setSpacing",new Class[] {boolean.class}));
    va.addAttr("margin",c.getMethod("setMargin",new Class[] {Layout.MarginInfo.class}));
  }

  private void setupVerticalLayout() throws Exception {
    Class c = com.vaadin.ui.VerticalLayout.class;
    VulAttributes va = setupStandardContainer(c);
    va.addAttr("spacing",c.getMethod("setSpacing",new Class[] {boolean.class}));
    va.addAttr("margin",c.getMethod("setMargin",new Class[] {Layout.MarginInfo.class}));
  }

  private void setupAbsoluteLayout() throws Exception {
    Class c = com.vaadin.ui.AbsoluteLayout.class;
    VulAttributes va = setupStandardContainer(c);
    va.addAttr("margin",c.getMethod("setMargin",new Class[] {Layout.MarginInfo.class}));
  }

  private void setupCssLayout() throws Exception {
    Class c = com.vaadin.ui.CssLayout.class;
    VulAttributes va = setupStandardContainer(c);
    va.addAttr("margin",c.getMethod("setMargin",new Class[] {Layout.MarginInfo.class}));
  }

  private void setupFormLayout() throws Exception {
    Class c = com.vaadin.ui.FormLayout.class;
    VulAttributes va = setupStandardContainer(c);
    va.addAttr("spacing",c.getMethod("setSpacing",new Class[] {boolean.class}));
    va.addAttr("margin",c.getMethod("setMargin",new Class[] {Layout.MarginInfo.class}));
    va.addAttr("orientation",c.getMethod("setOrientation",new Class[] {int.class}));
  }
}
